from django.http import FileResponse, JsonResponse
from django.shortcuts import render
from rest_framework import viewsets, status
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.views import APIView
from storage import serializers
from rest_framework.permissions import IsAuthenticated, AllowAny
from storage.permissions import IsOwner, IsAccess
# Create your views here.


class RegisterView(APIView):
    permission_classes = [AllowAny]
    def post(self, request):
        serializer = serializers.RegisterSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        token, _ = Token.objects.get_or_create(user=user)
        return Response({
            'success': True,
            'message': 'Success',
            'token': token.key
        })


class LoginView(APIView):
    permission_classes = [AllowAny]
    def post(self, request):
        serializer = serializers.LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        token, _ = Token.objects.get_or_create(user=user)
        return Response({
            'success': True,
            'message': 'Success',
            'token': token.key
        })


class LogoutView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        Token.objects.filter(user=request.user).delete()
        return Response({
            'success': True,
            "message": "Logout",
        })


class FileUploadView(APIView):
    permission_classes = [IsAuthenticated]
    def post(self, request):
        serializer = serializers.FileUploadSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)

class FileView(APIView):
    def get_permissions(self):
        if self.request.method == "GET":
            return [IsAccess()]
        return [IsOwner()]

    def get(self, request, file_id):
        serializer = serializers.FileDownloadSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user, file_id=file_id)
        return FileResponse(response.file, filename=response.name, as_attachment=True)

    def patch(self, request, file_id):
        serializer = serializers.FileRenameSerilizer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user, file_id=file_id)
        return Response(response)

    def delete(self, request, file_id):
        serializer = serializers.FileDeleteSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user, file_id=file_id)
        return Response(response)


class AccessView(APIView):
    permission_classes = [IsOwner]
    def post(self, request, file_id):
        serializer = serializers.AddAccessSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user, file_id=file_id)
        return Response(response)

    def delete(self, request, file_id):
        serializer = serializers.RemoveAccessSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user, file_id=file_id)
        return Response(response)


class UserFilesView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        serializer = serializers.UserFilesSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)


class UserAccesses(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        serializer = serializers.UserAccessesSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        response = serializer.save(user=request.user)
        return Response(response)


def custom404(request, exception=None):
    return JsonResponse({
        'message': 'Not Found'
    }, status=status.HTTP_404_NOT_FOUND)